package org.jim.server.demo.service.processor;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jim.core.ImChannelContext;
import org.jim.core.packets.Message;
import org.jim.core.packets.UserStatusType;
import org.jim.server.demo.entity.ImUserInfo;
import org.jim.server.demo.service.impl.ImUserInfoServiceImpl;
import org.jim.server.processor.login.LoginCmdProcessor;
import org.jim.server.processor.login.LogoutCmdProcessor;
import org.jim.server.protocol.AbstractProtocolCmdProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class LogoutServiceProcessor extends AbstractProtocolCmdProcessor implements LogoutCmdProcessor {

    private static final Logger log = LoggerFactory.getLogger(LogoutServiceProcessor.class);

    @Resource
    private ImUserInfoServiceImpl imUserInfoService;
    /**
     * 退出登录操作
     */
    @Override
    public void doLogout(String userId, ImChannelContext imChannelContext) {

        log.info("执行退出登录操作！");

        QueryWrapper<ImUserInfo> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);

        ImUserInfo imUserInfo = imUserInfoService.getOne(qw);

        if(ObjectUtil.isNull(imUserInfo)){
            log.info("该用户不存在！！");
        }else{
            imUserInfo.setUserState(UserStatusType.OFFLINE.getStatus());

            imUserInfoService.updateById(imUserInfo);
        }

    }

}
